<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,index) in RoleList" :key="item.roleId" class="role-item" :class="{active:activeIndex===index}" @click="changeActiveIndex(index)">
        <div class="role-info">
          <svg-icon :icon-class="activeIndex===index?'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="first">
          <div class="tree-wrapper">
            <div v-for="item in TreeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                node-key="id"
                show-checkbox
                default-expand-all
              />
            </div>
          </div></el-tab-pane>
        <el-tab-pane label="成员" name="second">
          <div class="user-wrapper">
            <el-table
              :data="rememberList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table></div></el-tab-pane>
      </el-tabs>
    </div>
  </div></template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI, delRoleUserAPI } from '@/api/system'
function AddDisabled(TreeList) {
  TreeList.forEach(item => {
    // 给每一个节点添加disabled属性
    item.disabled = true
    // 判断是否有字节点如果有子节点则继续递归，直到没有字节点
    if (item.children) {
      AddDisabled(item.children)
    }
  })
}
export default {
  name: 'Role',
  data() {
    return {
      // 激活状态
      activeIndex: 0,
      // 角色列表
      RoleList: [],
      TreeList: [],
      rememberList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      permsList: [],
      // tags切换
      activeName: 'first'
    }
  },
  async mounted() {
    // 获取所有角色
    await this.getRoleList()
    // 获取树形列表信息
    await this.getTreeList()
    // 默认选中状态
    this.changeActiveIndex(0)
  },
  methods: {
    // 获取角色信息
    async getRoleList() {
      // 获取所有角色信息，对角色信息发起请求,
      const res = await getRoleListAPI()
      this.RoleList = res.data
    },
    // 获取树形列表信息
    async getTreeList() {
      const res = await getTreeListAPI()
      this.TreeList = res.data
      // 获取到树形列表的时候需要全部启用禁用状态
      AddDisabled(this.TreeList)
    },
    /** --------------------------------------------------------- */
    // 改变高亮显示
    async changeActiveIndex(index) {
      // 切换高亮信息
      this.activeIndex = index
      // 通过index获取角色id
      const roleId = this.RoleList[index].roleId
      // 通过id调用接口获取到权限数据并添加disabled属性
      this.AddDisabled(roleId)
      // 获取成员信息
      this.addRemember(roleId)
    },
    async AddDisabled(roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.permsList = res.data.perms
      // 通过获取到的数据自动添加选中状态
      this.$refs.tree.forEach((item, index) => {
        item.setCheckedKeys(this.permsList[index])
      })
    },
    async addRemember(roleId) {
      const resmember = await getRoleUserAPI(roleId)
      this.rememberList = resmember.data.rows
    },
    // 修改用户信息
    editRole(id) {
      console.log(id)
      this.$router.push({
        path: '/editRole',
        query: {
          id
        }
      })
    },
    // 删除用户
    delRole(roleId) {
      this.$confirm('此操作将永久该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleUserAPI(roleId)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 获取所有角色
        await this.getRoleList()
        // 获取树形列表信息
        await this.getTreeList()
        // 默认选中状态
        this.changeActiveIndex(0)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
